package com.alstudio.utils.android.media;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Vibrator;

import com.alstudio.utils.log.Mylog;
import com.alstudio.utils.notification.SystemNotificationEnvConfig.SystemSettingsState;

public class ALMediaManager {

	// 震动管理器
	private Vibrator mVibrator;
	// 此实例仅供播放铃声使用
	private MediaPlayer mediaPlayer = null;
	private Context ctx;
	private int defaultRing = 0;
	private static ALMediaManager instance;

	public ALMediaManager(Context ctx) {
		// TODO Auto-generated constructor stub
		this.ctx = ctx;
		mVibrator = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
	}

	public static ALMediaManager getInstance(Context ctx) {
		if (instance == null) {
			instance = new ALMediaManager(ctx);
		}
		return instance;
	}

	public void setDefaultRing(int ring) {
		this.defaultRing = ring;
	}

	public int getDefaultRing() {
		return defaultRing;
	}

	public void playMusic(Context context, String path) {
		
		
		
	}

	public void playMusic(Context context, Uri uri) {
		mediaPlayer = MediaPlayer.create(context, uri);
		if (mediaPlayer != null) {

			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					//播放已完成
					if (mediaPlayer != null) {
						mediaPlayer.stop();
						mediaPlayer.release();
						mediaPlayer = null;
					}

				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					//播放失败
					if (mediaPlayer != null) {
						mediaPlayer.stop();
						mediaPlayer.release();
						mediaPlayer = null;
					}

					return false;
				}
			});
			mediaPlayer.start();
		}
	}

	/**
	 * 播放铃声
	 * 
	 * @param ring
	 *            铃声资源
	 */
	private void playrRing() {
		mediaPlayer = MediaPlayer.create(ctx, defaultRing);

		if (mediaPlayer != null) {
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub

					// 播放完成后将播放器释放并将将当前播放的语音表情的message id置为空
					if (mediaPlayer != null) {
						mediaPlayer.stop();
						mediaPlayer.release();
						mediaPlayer = null;
					}

				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub

					// 播放完成后将播放器释放并将将当前播放的语音表情的message id置为空
					if (mediaPlayer != null) {
						mediaPlayer.stop();
						mediaPlayer.release();
						mediaPlayer = null;
					}

					return false;
				}
			});
			mediaPlayer.start();
		}
	}

	/**
	 * 播放铃声
	 * 
	 * @param ring
	 *            铃声资源
	 */
	private void playrRing(int ring) {
		mediaPlayer = MediaPlayer.create(ctx, ring);

		if (mediaPlayer != null) {
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub

					// 播放完成后将播放器释放并将将当前播放的语音表情的message id置为空
					if (mediaPlayer != null) {
						mediaPlayer.stop();
						mediaPlayer.release();
						mediaPlayer = null;
					}

				}
			});

			mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub

					// 播放完成后将播放器释放并将将当前播放的语音表情的message id置为空
					if (mediaPlayer != null) {
						mediaPlayer.stop();
						mediaPlayer.release();
						mediaPlayer = null;
					}

					return false;
				}
			});
			mediaPlayer.start();
		}
	}

	private void playVibrate() {
		long[] vibrate = { 300, 100, 300, 100 };
		mVibrator.vibrate(vibrate, -1);
	}

	private void playVibrate(long[] vibrate) {
		mVibrator.vibrate(vibrate, -1);
	}

	public void playRingAndVibrate(SystemSettingsState state, int ring) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playrRing(ring);
			playVibrate();
			Mylog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playrRing(ring);
			Mylog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate();
			Mylog.d("vibrate only");
			break;
		case MUTE_TIME:
			Mylog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			Mylog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	public void playRingAndVibrate(SystemSettingsState state) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playrRing();
			playVibrate();
			Mylog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playrRing();
			Mylog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate();
			Mylog.d("vibrate only");
			break;
		case MUTE_TIME:
			Mylog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			Mylog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

	public void playRingAndVibrate(SystemSettingsState state, int ring,
			long[] vibrate) {
		switch (state) {
		case SOUND_AND_VIBRATION_ALL_ON:
			playrRing();
			playVibrate(vibrate);
			Mylog.d("ring and vibrate all on");
			break;
		case SOUND_ONLY:
			playrRing();
			Mylog.d("ring only");
			break;
		case VIBRATION_ONLY:
			playVibrate(vibrate);
			Mylog.d("vibrate only");
			break;
		case MUTE_TIME:
			Mylog.d("mute time...");
			break;
		case SOUND_AND_VIBRATION_ALL_OFF:
			Mylog.d("ring and vibrate all off");
			break;
		default:
			break;
		}
	}

}
